//
//  doctorsynopsisController.m
//  FastDoctor
//
//  Created by 云卷云舒丶 on 16/7/8.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "doctorsynopsisController.h"
#import <UIImageView+WebCache.h>
#import "DoctorInfoCell.h"
#import "HeadImageView.h"

@interface doctorsynopsisController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, weak) HeadImageView *docImg;
@property (nonatomic, weak) UILabel *lb_name;
@property (nonatomic, weak) UILabel *lb_post;
@property (nonatomic, weak) UILabel *lb_hospital;
@property (nonatomic, weak) UIButton * operation;
@property (nonatomic, weak) UIButton * flower;
@property (nonatomic, weak) UIButton * banner;
@property (nonatomic, weak) UIView *grayView;

@property(nonatomic,strong)NSArray<attentionDoctorsModel *>* dataArray;


//中间那条竖线
@property (nonatomic,weak) UIView *lineView;

//就诊条件
@property (nonatomic,weak)UIButton *conditionButton;
//医生简介
@property (nonatomic,weak)UIButton *doctorDetailButton;

//增加一条可以运动的横线
@property (nonatomic,weak)UIView *moveLine;

//最后的咨询医生按钮
@property (nonatomic,weak)UIButton *askDocotorButton;


//colleCtionView
@property (nonatomic,weak)UICollectionView *doctorCollectionView;

//colleCtionView的flowLayout
@property (nonatomic,weak)UICollectionViewFlowLayout *flowLayout;


@end

@implementation doctorsynopsisController


static NSString *collectionIdentifier = @"collectCell";


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"医生的基本信息";
    self.view.backgroundColor = [UIColor whiteColor];
    
    //上面的view
    [self setupUI];
    
    //中间的两个按钮
    [self setupButtons];
    
    //最后的咨询医生按钮
    [self setUpAskDoctor];
    
    //中间的CollecCtionView
    [self setUpCollection];
    
    
}


//上面的view
-(void)setupUI{
    
    
    //设置一条灰色的长线
    UIView *grayView = [[UIView alloc] init];
    
    self.grayView = grayView;
    
    grayView.backgroundColor = [UIColor colorWithRed:202/265.0 green:202/265.0 blue:202/265.0 alpha:1.0];
    
    [self.view addSubview:grayView];
    
    //设置grayView约束
    [grayView makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.equalTo(@3);
        make.top.equalTo(self.view).offset(self.view.bounds.size.height*0.3);
    }];
    
    
    
    
    
    //头像
    HeadImageView *docImg = [[HeadImageView alloc]init];
    self.docImg = docImg;
    [self.view addSubview:_docImg];
    
    [self.docImg sd_setImageWithURL:[NSURL URLWithString:self.model.doctor_portrait] placeholderImage:[UIImage imageNamed:@"name"]];
    
    
    
    [docImg makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(75);
        make.left.equalTo(self.view).offset(15);
        make.bottom.equalTo(grayView.mas_bottom).offset(-50);
        make.width.equalTo(docImg.height);

    }];
    
    
    //医生名
    UILabel *lb_name = [[UILabel alloc]init];
    self.lb_name = lb_name;
    [self.view addSubview:_lb_name];
    
    self.lb_name.text = self.model.doctor_name;
    [self.lb_name setFont:[UIFont systemFontOfSize:16]];
    
    [lb_name makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(docImg.right).offset(20);
        make.top.equalTo(self.view).offset(75);
        
    }];
    
    
    
    
    
    
    
    
    //医生类型
    UILabel *lb_post = [[UILabel alloc]init];
    self.lb_post = lb_post;
    [self.view addSubview:lb_post];
    
    self.lb_post.text = self.model.doctor_title_name;
    [self.lb_post setTextColor:[UIColor colorWithRed:202/265.0 green:202/265.0 blue:202/265.0 alpha:1.0]];
    [self.lb_post setFont:[UIFont systemFontOfSize:15]];
    
    [lb_post makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(lb_name.right).offset(10);
        make.top.equalTo(self.view).offset(75);
        
    }];
    
    
    
    
    
    
    
    //医院名
    UILabel *lb_hospital = [[UILabel alloc]init];
    self.lb_hospital = lb_hospital;
    [self.view addSubview:lb_hospital];
    
    self.lb_hospital.text = self.model.doctor_hospital_name;
    [self.lb_hospital setTextColor:[UIColor colorWithRed:202/265.0 green:202/265.0 blue:202/265.0 alpha:1.0]];
    [self.lb_hospital setFont:[UIFont systemFontOfSize:16]];
    
    
    [lb_hospital makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(docImg.right).offset(20);
        make.top.equalTo(lb_name.bottom).offset(20);
    }];
    
    
    
    
    
    
    
    
    //加号
    UIButton *operation = [[UIButton alloc]init];
    self.operation = operation;
    [self.view addSubview:operation];
    
    
    [self.operation setImage:[UIImage imageNamed:@"yuyueliang"] forState:UIControlStateNormal];
    [self.operation setTitle:[NSString stringWithFormat:@"  %@",self.model.operation_count] forState:UIControlStateNormal];
    [self.operation setTitleColor:kThemeColor forState:UIControlStateNormal];
    
    
    self.operation.enabled = NO;
    
    
    [operation makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(grayView.bottom).offset(-20);
        make.centerX.equalTo(docImg.centerX);
    }];
    
    
    
    
    
    
    
    
    //花
    UIButton *flower = [[UIButton alloc]init];
    self.flower = flower;
    [self.view addSubview:flower];
    
    
    [self.flower setImage:[UIImage imageNamed:@"xianhua"] forState:UIControlStateNormal];
    [self.flower setTitle:[NSString stringWithFormat:@"  %@",self.model.flower] forState:UIControlStateNormal];
    [self.flower setTitleColor:kThemeColor forState:UIControlStateNormal];
    
    self.flower.enabled = NO;
    
    
    [flower makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(grayView.bottom).offset(-20);
        make.centerX.equalTo(self.view.centerX);
    }];
    
    
    
    
    
    
    
    
    //旗
    UIButton *banner = [[UIButton alloc]init];
    self.banner = banner;
    [self.view addSubview:banner];
    
    [self.banner setImage:[UIImage imageNamed:@"jinqi"] forState:UIControlStateNormal];
    [self.banner setTitle:[NSString stringWithFormat:@"  %@",self.model.banner] forState:UIControlStateNormal];
    [self.banner setTitleColor:kThemeColor forState:UIControlStateNormal];
    
    
    
    self.banner.enabled = NO;
    
    [banner makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(grayView.bottom).offset(-20);
        make.right.equalTo(self.view.right).offset(-15-40);
    }];
    
    

}


//中间的两个按钮
-(void)setupButtons{
    
    
    //(1)中间那条竖线
    UIView *lineView = [[UIView alloc] init];
    self.lineView = lineView;
    lineView.backgroundColor = [UIColor colorWithRed:202/265.0 green:202/265.0 blue:202/265.0 alpha:1.0];
    [self.view addSubview:lineView];
    
    [lineView makeConstraints:^(MASConstraintMaker *make) {
       
       make.centerX.equalTo(self.view.centerX);
        
        if (kDevice_Is_iPhone6Plus)
        {
            make.width.equalTo(@2);
        }
        else
        {
             make.width.equalTo(@1);
        }
        make.height.equalTo(@35);
        make.top.equalTo(self.grayView.mas_bottom).offset(10);
    }];
    
    //(2)左边那个按钮"就诊条件"
    UIButton *conditionButton = [[UIButton alloc] init];
    self.conditionButton = conditionButton;
    [conditionButton setTitle:@"就诊条件" forState:UIControlStateNormal];
    [conditionButton setTitleColor:kThemeColor forState:UIControlStateNormal];
    
    conditionButton.tag = 0;
    
    [self.view addSubview:conditionButton];
    
    
    [conditionButton makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.grayView.mas_bottom).offset(15);
        if (kDevice_Is_iPhone5) {
            make.right.equalTo(lineView.mas_left).offset(-30);
            make.left.equalTo(self.view).offset(30);
        }else{
        make.right.equalTo(lineView.mas_left).offset(-50);
        make.left.equalTo(self.view).offset(50);
        }
    }];
    
    //增加点击按钮的触发事件
    [conditionButton addTarget:self action:@selector(ClickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    //(3)右边那个按钮"医生简介"
    UIButton *doctorDetailButton = [[UIButton alloc] init];
    self.doctorDetailButton = doctorDetailButton;
    [doctorDetailButton setTitle:@"医生简介" forState:UIControlStateNormal];
    [doctorDetailButton setTitleColor:[UIColor colorWithRed:202/265.0 green:202/265.0 blue:202/265.0 alpha:1.0] forState:UIControlStateNormal];
    doctorDetailButton.tag = 1;
    
    [self.view addSubview:doctorDetailButton];
    
    
    [doctorDetailButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.grayView.mas_bottom).offset(15);
        if (kDevice_Is_iPhone5) {
            make.left.equalTo(lineView.mas_right).offset(30);
            make.right.equalTo(self.view).offset(-30);
        }else{
        make.left.equalTo(lineView.mas_right).offset(50);
        make.right.equalTo(self.view).offset(-50);
        }
    }];
    
    //增加点击按钮的触发事件
    [doctorDetailButton addTarget:self action:@selector(ClickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    //(4)增加一条可以运动的横线
    UIView *moveLine = [[UIView alloc] init];
    self.moveLine = moveLine;
    moveLine.backgroundColor = kThemeColor;
    [self.view addSubview:moveLine];
    
    [moveLine makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(conditionButton.mas_bottom).offset(5);
        make.centerX.equalTo(conditionButton.centerX);
        make.height.equalTo(@2);
        make.left.equalTo(self.view).offset(50);
    }];
    
}

//点击按钮的触发事件
-(void)ClickButton:(UIButton *)button
{
    //判断点击的是哪一个按钮
    [self setButtonState:button.tag];
    
    //collection滚动到第二个cell
    [self.doctorCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:button.tag inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}

//点击按钮的触发事件
-(void)setButtonState:(NSInteger) index
{
    //判断点击的是哪一个按钮
    if (index == 0)
    {
        //NSLog(@"点击了就诊条件");
        [self.conditionButton setTitleColor:kThemeColor forState:UIControlStateNormal];
        [self.doctorDetailButton setTitleColor:[UIColor colorWithRed:202/265.0 green:202/265.0 blue:202/265.0 alpha:1.0] forState:UIControlStateNormal];
        self.moveLine.transform = CGAffineTransformIdentity;
    }
    else
    {
        //NSLog(@"点击了医生简介");
        [self.doctorDetailButton setTitleColor:kThemeColor forState:UIControlStateNormal];
        [self.conditionButton setTitleColor:[UIColor colorWithRed:202/265.0 green:202/265.0 blue:202/265.0 alpha:1.0] forState:UIControlStateNormal];
        
        //moveLine滑动
        self.moveLine.transform =CGAffineTransformMakeTranslation(self.view.bounds.size.width*0.5, 0);
    }
}


//最后的咨询医生按钮
-(void)setUpAskDoctor
{
    UIButton *askDocotorButton = [[UIButton alloc] init];
    self.askDocotorButton = askDocotorButton;
    
    [askDocotorButton setBackgroundColor:kThemeColor];
    [askDocotorButton setTitle:@"咨询医生" forState:UIControlStateNormal];
    
    [self.view addSubview:askDocotorButton];
    
    [askDocotorButton makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.bottom.equalTo(self.view.bottom).offset(-10);
        make.height.equalTo(@40);
    }];
    
}

//中间的CollecCtionView
-(void)setUpCollection
{
    //(1)实例化一个流水型布局对象
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.flowLayout = flowLayout;

    
    //设置行间距
    flowLayout.minimumLineSpacing = 0;
    
    //设置列间距
    flowLayout.minimumInteritemSpacing = 0;
    
    
    //设置cell的滚动方向(默认为垂直方向)
    //滚动方向改变之后,行内距和列内距设置就相反了
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //(2)利用flowLayout来实例化一个collectionView
    //暂时把frame设置为zero，待会利用手动方式设置Auto Layout
    UICollectionView *doctorCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    
    //设置背景
    doctorCollectionView.backgroundColor = [UIColor whiteColor];
    
    self.doctorCollectionView = doctorCollectionView;
    
    doctorCollectionView.pagingEnabled = YES;
    doctorCollectionView.bounces = NO;
    doctorCollectionView.showsHorizontalScrollIndicator = NO;
    
    
    [self.view addSubview:doctorCollectionView];
    
    //(3)设置collectionView的layout
    [doctorCollectionView makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.moveLine.bottom).offset(5);
        make.bottom.equalTo(self.askDocotorButton.top).offset(-3);
    }];

    //设置代理
    doctorCollectionView.dataSource = self;
    
    doctorCollectionView.delegate = self;
    
    
    //必须使用注册的方式来使用collection的cell
    [doctorCollectionView registerClass:[DoctorInfoCell class] forCellWithReuseIdentifier:collectionIdentifier];
}

//布局子控件时设置flowLayout
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    self.flowLayout.itemSize = self.doctorCollectionView.frame.size;
    
}


//实现collection的代理方法
//(1)组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//(2)item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}
//(3)item上的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DoctorInfoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionIdentifier forIndexPath:indexPath];
    
    
    cell.num = indexPath.item;
    
    return cell;
}

//(4)当手动拖拽collectionView时的触发事件
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat scale = scrollView.contentOffset.x / self.doctorCollectionView.bounds.size.width;
    
    
    //moveLine运动
    _moveLine.transform = CGAffineTransformMakeTranslation(self.view.bounds.size.width*scale*0.5, 0);
}

//(5)当手动拖拽完成之后的触发事件
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/self.doctorCollectionView.bounds.size.width;

    //NSLog(@"%ld",(long)index);
    
    [self setButtonState:index];
}
@end
